{% extends "base.html" %}

{% block head %}
    <script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js" async></script>
{% endblock head %}

{% block title %}会员管理{% endblock title %}
{% block subtitle %}成员 {{ '校正' if member else '添加' }}{% endblock subtitle %}

{% block content %}
<form name="fmember" id="fmember" action="/admin/member_form_update" onsubmit="return fmember_submit(this);" method="post" enctype="multipart/form-data" autocomplete="off">
<input type="hidden" name="token" value="">

<div class="tbl_frm01 tbl_wrap">
    <table>
        <caption>成员 {{ '校正' if member else '添加' }}</caption>
        <colgroup>
            <col class="grid_4">
            <col>
            <col class="grid_4">
            <col>
        </colgroup>
        <tbody>
            <tr>
                <th scope="row"><label for="mb_id">Idie<strong class="blind">必不可少的</strong></label></th>
                <td>
                    {% if member.mb_id %}
                        <input type="hidden" name="mb_id" value="{{ member.mb_id }}" id="mb_id">
                        <b>{{ member.mb_id }}</b>
                        <a href="/admin/boardgroupmember_form/{{ member.mb_id }}" class="btn_frmline">请参阅可访问组</a>
                    {% else %}
                        <input type="text" name="mb_id" value="" id="mb_id" required class="frm_input required alnum_" size="15" maxlength="20">
                    {% endif %}
                </td>
                <th scope="row"><label for="mb_password">密码<strong class="blind">必不可少的</strong></label></th>
                <td>
                    <div>
                    {% set mb_password_required = 'required' if not member else '' %}
                    <input type="password" name="mb_password" id="mb_password" class="frm_input {{ mb_password_required }}" size="15" maxlength="20" {{ mb_password_required }}>
                    </div>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_name">姓名(真实姓名)<strong class="blind">必不可少的</strong></label></th>
                <td><input type="text" name="mb_name" value="{{ member.mb_name }}" id="mb_name" required class="required frm_input" size="15" maxlength="20"></td>
                <th scope="row"><label for="mb_nick">昵称<strong class="blind">必不可少的</strong></label></th>
                <td><input type="text" name="mb_nick" value="{{ member.mb_nick }}" id="mb_nick" required class="required frm_input" size="15" maxlength="20"></td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_level">成员权力</label></th>
                <td>
                    {{ get_member_level_select('mb_level', 2, 10, member.mb_level)|safe }}
                </td>
                <th scope="row">点</th>
                <td>
                    {% if member %}
                        <a href="/admin/point_list?sfl=mb_id&stx={{ member.mb_id }}" target="_blank">{{ member.mb_point|number_format }}</a> 圆点
                    {% else %}
                        <a href="/admin/config_form#anc_cf_join" target="_blank">{{ request.state.config.cf_register_point }}</a> 圆点
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_email">E-mail<strong class="blind">必不可少的</strong></label></th>
                <td><input type="text" name="mb_email" value="{{ member.mb_email }}" id="mb_email" maxlength="100" required class="required frm_input email" size="30"></td>
                <th scope="row"><label for="mb_homepage">主页</label></th>
                <td><input type="text" name="mb_homepage" value="{{ member.mb_homepage }}" id="mb_homepage" class="frm_input" maxlength="255" size="15"></td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_hp">移动电话号码</label></th>
                <td><input type="text" name="mb_hp" value="{{ member.mb_hp }}" id="mb_hp" class="frm_input" size="15" maxlength="20"></td>
                <th scope="row"><label for="mb_tel">电话号码</label></th>
                <td><input type="text" name="mb_tel" value="{{ member.mb_tel }}" id="mb_tel" class="frm_input" size="15" maxlength="20"></td>
            </tr>
            <tr>
                <th scope="row">标识</th>
                <td>
                    {% if member.mb_certify == '' %}未经认证{% endif %}
                    {% if member.mb_certify == 'simple' %}简单身份验证{% endif %}
                    {% if member.mb_certify == 'hp' %}移动电话{% endif %}
                    {% if member.mb_certify == 'ipin' %}ipin{% endif %}
                {#
                    <!-- mb_certify_case不被使用-->
                    <input type="radio" name="mb_certify_case" value="simple" id="mb_certify_sa" {{ 'checked' if member.mb_certify == 'simple' }}>
                    <label for="mb_certify_sa">简单身份验证</label>
                    <input type="radio" name="mb_certify_case" value="hp" id="mb_certify_hp" {{ 'checked' if member.mb_certify == 'hp' }}>
                    <label for="mb_certify_hp">移动电话</label>
                    <input type="radio" name="mb_certify_case" value="ipin" id="mb_certify_ipin" {{ 'checked' if member.mb_certify == 'ipin' }}>
                    <label for="mb_certify_ipin">ipin</label>
                #}
                </td>

                <th scope="row">成人认证</th>
                <td>
                    {#
                    <input type="radio" name="mb_adult" value="1" id="mb_adult_yes" {{ 'checked' if member.mb_adult }}>
                    <label for="mb_adult_yes">是</label>
                    <input type="radio" name="mb_adult" value="0" id="mb_adult_no" {{ 'checked' if not member.mb_adult }}>
                    <label for="mb_adult_no">没有。</label>
                    #}
                    {{ '是' if member.mb_adult else '没有。' }}
                </td>
            </tr>
            {#
            <tr>
                <th scope="row">检查你自己的</th>
                <td>
                    <input type="radio" name="mb_certify" value="1" id="mb_certify_yes" {{ 'checked' if member.mb_certify else '' }}>
                    <label for="mb_certify_yes">是</label>
                    <input type="radio" name="mb_certify" value="0" id="mb_certify_no" {{ 'checked' if not member.mb_certify else '' }}>
                    <label for="mb_certify_no">没有。</label>
                </td>
            </tr>
            #}
            <tr>
                <th scope="row">地址</th>
                <td colspan="3" class="td_addr_line">
                    <label for="mb_zip" class="blind">邮政号码</label>
                    <input type="text" name="mb_zip" value="{{ member.mb_zip1 ~ member.mb_zip2 }}" id="mb_zip" class="frm_input readonly" size="5" maxlength="6">
                    <button type="button" class="btn_frmline" onclick="win_zip('fmember', 'mb_zip', 'mb_addr1', 'mb_addr2', 'mb_addr3', 'mb_addr_jibeon');">搜索地址</button><br>
                    <input type="text" name="mb_addr1" value="{{ member.mb_addr1 }}" id="mb_addr1" class="frm_input readonly" size="60">
                    <label for="mb_addr1">基本地址</label><br>
                    <input type="text" name="mb_addr2" value="{{ member.mb_addr2 }}" id="mb_addr2" class="frm_input" size="60">
                    <label for="mb_addr2">详细地址</label>
                    <br>
                    <input type="text" name="mb_addr3" value="{{ member.mb_addr3 }}" id="mb_addr3" class="frm_input" size="60">
                    <label for="mb_addr3">备注项目</label>
                    <input type="hidden" name="mb_addr_jibeon" value=""><br>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_icon">成员图标</label></th>
                <td colspan="3">
                    <span class="frm_info">图像大小为 <strong>宽 22像素高度 22像素</strong>所以请这样做。.</span>
                    <span class="frm_info">图像大小为 <strong>宽 22像素高度 22像素</strong>所以请这样做。.</span>
                    <input type="file" name="mb_icon" id="mb_icon">
                    {% if member.mb_icon %}
                        <img src="{{ member.mb_icon|safe }}" title="{{ member.mb_id }}Nim成员图标" target="_blank" 
                            width="{{ request.state.config.cf_member_icon_width }}" height="{{ request.state.config.cf_member_icon_height }}">
                        <label><input type="checkbox" name="del_mb_icon" value="1" id="del_mb_icon"> 删除</label>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_img">成员图像</label></th>
                <td colspan="3">
                    <span class="frm_info">图像大小为 <strong>宽 60像素高度 60像素</strong>所以请这样做。.</span>
                    <input type="file" name="mb_img" id="mb_img">
                    {% if member.mb_img %}
                        <img src="{{ member.mb_img|safe }}" title="{{ member.mb_id }}Nim成员图标" target="_blank"
                            width="{{ request.state.config.cf_member_img_width }}" height="{{ request.state.config.cf_member_img_height }}">
                        <label><input type="checkbox" name="del_mb_img" value="1" id="del_mb_icon"> 删除</label>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <th scope="row">接收邮件</th>
                <td>
                    <input type="radio" name="mb_mailling" value="1" id="mb_mailling_yes" {{ 'checked' if member.mb_mailling }}>
                    <label for="mb_mailling_yes">是</label>
                    <input type="radio" name="mb_mailling" value="0" id="mb_mailling_no" {{ 'checked' if not member.mb_mailling }}>
                    <label for="mb_mailling_no">没有。</label>
                </td>
                <th scope="row"><label for="mb_sms_yes">SMS 收货</label></th>
                <td>
                    <input type="radio" name="mb_sms" value="1" id="mb_sms_yes" {{ 'checked' if member.mb_sms }}>
                    <label for="mb_sms_yes">是</label>
                    <input type="radio" name="mb_sms" value="0" id="mb_sms_no" {{ 'checked' if not member.mb_sms }}>
                    <label for="mb_sms_no">没有。</label>
                </td>
            </tr>
            <tr>
                <th scope="row">信息披露</th>
                <td colspan="3">
                    <input type="radio" name="mb_open" value="1" id="mb_open_yes" {{ 'checked' if member.mb_open }}>
                    <label for="mb_open_yes">是</label>
                    <input type="radio" name="mb_open" value="0" id="mb_open_no" {{ 'checked' if not member.mb_open }}>
                    <label for="mb_open_no">没有。</label>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_signature">签名</label></th>
                <td colspan="3"><textarea name="mb_signature" id="mb_signature">{{ member.mb_signature }}</textarea></td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_profile">自我介绍</label></th>
                <td colspan="3"><textarea name="mb_profile" id="mb_profile">{{ member.mb_profile }}</textarea></td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_memo">备注</label></th>
                <td colspan="3"><textarea name="mb_memo" id="mb_memo">{{ member.mb_memo }}</textarea></td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_cert_history">证书历史记录</label></th>
                <td colspan="3">我没有我的身份验证。.</td>
            </tr>
            <tr>
                <th scope="row"><label for="mb_leave_date">撤回日</label></th>
                <td>
                    <input type="text" name="mb_leave_date" value="{{ member.mb_leave_date }}" id="mb_leave_date" class="frm_input" maxlength="8">
                    <input type="checkbox" value="{{ today }}" id="mb_leave_date_set_today" onclick="if (this.form.mb_leave_date.value==this.form.mb_leave_date.defaultValue) { this.form.mb_leave_date.value=this.value; } else { this.form.mb_leave_date.value=this.form.mb_leave_date.defaultValue; }">
                    <label for="mb_leave_date_set_today">提款日指定为今天</label>
                </td>
                <th scope="row">访问阻止日</th>
                <td>
                    <input type="text" name="mb_intercept_date" value="{{ member.mb_intercept_date }}" id="mb_intercept_date" class="frm_input" maxlength="8">
                    <input type="checkbox" value="{{ today }}" id="mb_intercept_date_set_today" onclick="if (this.form.mb_intercept_date.value==this.form.mb_intercept_date.defaultValue) { this.form.mb_intercept_date.value=this.value; } else { this.form.mb_intercept_date.value=this.form.mb_intercept_date.defaultValue; }">
                    <label for="mb_intercept_date_set_today">指定访问阻止日为今天</label>
                </td>
            </tr>
            {% for i in range(1, 11) %}
            <tr>
                <th scope="row"><label for="mb_{{ i }}">额外字段 {{ i }}</label></th>
                <td colspan="3"><input type="text" name="mb_{{ i }}" value="{{ getattr(member, 'mb_' ~ i) if member else "" }}" id="mb_{{ i }}" class="frm_input" size="30" maxlength="255"></td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <div class="btn_fixed_top">
        <a href="/admin/member_list?sst=&amp;sod=&amp;sfl=&amp;stx=&amp;page=" class="btn btn_02">列表</a>
        <input type="submit" value="检查" class="btn_submit btn" accesskey='s'>
    </div>
</form>

<script>
    function check_member_id(mb_id) {
        return fetch(`/admin/check_member_id/${mb_id}`)
            .then(response => response.json())
            .then(data => data.result === 'exists')
            .catch(error => {
                console.error('Error:', error);
                return false;
            });
    }
    
    function fmember_submit(f) {
        if (!f.mb_icon.value.match(/\.({{ request.state.config.cf_image_extension }})$/i) && f.mb_icon.value) {
            alert('图标是 {{ request.state.config.cf_image_extension|replace("|", ", ") }} 只有文件是可能的.');
            return false;
        }

        if (!f.mb_img.value.match(/\.({{ request.state.config.cf_image_extension }})$/i) && f.mb_img.value) {
            alert('成员图像是 {{ request.state.config.cf_image_extension|replace("|", ", ") }} 只有文件是可能的.');
            return false;
        }

        {% if not member.mb_id %}
        // ID重复验证
        var mb_id = f.mb_id.value;
        var is_duplicated = false; // 用于存储重复状态的变量
        
        $.ajax({
            <!-- url: '/admin/check_member_id/'+mb_id, -->
            url: `/admin/check_member_id/${mb_id}`,
            type: 'GET',
            async: false,
            cache: false,
            success: function(data) {
                if (data.result === 'exists') {
                    alert('这是一个已经存在的成员的Idy.');
                    is_duplicated = true;
                }
            },
            error: function(error) {
                console.error('Error:', error);
                alert('成员ID重复验证期间出现错误.')
                is_duplicated = true; // 如果发生错误，则将其视为重复状态
            }
        });
        if (is_duplicated) {
            f.mb_id.focus();
            return false; // 如果表单重复或有错误，请停止提交表单
        }
        {% endif %}

        // Nicknname重复验证
        if (f.mb_nick.value != '' && f.mb_nick.value != f.mb_nick.defaultValue) {
            var mb_id = f.mb_id.value;
            var mb_nick = f.mb_nick.value
            var is_duplicated = false; // 用于存储重复状态的变量

            $.ajax({
                url: `/admin/check_member_nick/${mb_nick}/${mb_id}`,
                type: 'GET',
                async: false,
                cache: false,
                success: function(data) {
                    if (data.result === 'exists') {
                        alert('这是一个重复的昵称。.');
                        is_duplicated = true;
                    }
                },
                error: function(error) {
                    console.error('Error:', error);
                    alert('验证昵称重复时出错.')
                    is_duplicated = true; // 如果发生错误，则将其视为重复状态
                    is_duplicated = true; // 如果发生错误，则将其视为重复状态
                }
            });
        }
        if (is_duplicated) {
            f.mb_nick.focus();
            return false; // 如果表单重复或有错误，请停止提交表单
        }

        // 电子邮件重复验证
        if (f.mb_email.value != '' && f.mb_email.value != f.mb_email.defaultValue) {
            var mb_id = f.mb_id.value;
            var mb_email = f.mb_email.value
            var is_duplicated = false; // 用于存储重复状态的变量

            $.ajax({
                url: `/admin/check_member_email/${mb_email}/${mb_id}`,
                type: 'GET',
                async: false,
                cache: false,
                success: function(data) {
                    if (data.result === 'exists') {
                        alert('这是一个重复的电子邮件地址.');
                        is_duplicated = true;
                    }
                },
                error: function(error) {
                    console.error('Error:', error);
                    alert('电子邮件重复验证期间出现错误.')
                    is_duplicated = true; // 如果发生错误，则将其视为重复状态
                }
            });
        }
        if (is_duplicated) {
            f.mb_email.focus();
            return false; // 如果表单重复或有错误，请停止提交表单
        }

        f.token.value = generate_token();
        f.action += "?{{ request.query_params|safe }}";

        return true;
    }
</script>
{% endblock content %}